package com.ruoyi.waterPrice.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.ruoyi.common.core.domain.base.datam.MiMainchanB;
import com.ruoyi.common.core.domain.business.waterDistribution.MiFarmlandB;
import com.ruoyi.common.core.domain.business.waterPrice.WiFarmacctY;
import com.ruoyi.common.core.domain.business.waterPrice.WiFarmbookR;
import com.ruoyi.common.core.domain.business.waterPrice.WiFarmfeeY;
import com.ruoyi.common.core.utils.FileUploadUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.waterPrice.mapper.*;
import com.ruoyi.waterPrice.service.IWiFarmacctYService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 农户水费台账登记Service业务层处理
 *
 * @author ps
 * @date 2023-02-17
 */
@Service
public class WiFarmacctYServiceImpl implements IWiFarmacctYService {
    @Autowired
    private WiFarmacctYMapper wiFarmacctYMapper;
    @Autowired
    private WiFarmfeeYMapper wiFarmfeeYMapper;
    @Autowired
    private WiFarmbookRMapper wiFarmbookRMapper;
    /**
     * 资源映射路径 前缀
     */
    @Value("${pzfile.prefix}")
    public String localFilePrefix;

    /**
     * 域名或本机访问地址
     */
    @Value("${pzfile.domain}")
    public String domain;

    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${pzfile.path}")
    private String localFilePath;

    /**
     * 查询农户水费台账登记
     *
     * @param feeYr 农户水费台账登记主键
     * @return 农户水费台账登记
     */
    @Override
    public WiFarmacctY selectWiFarmacctYByFeeYrAndFarmCd(String feeYr, String farmCd) {
        WiFarmacctY WiFarmacctY = wiFarmacctYMapper.selectWiFarmacctYByFeeYrAndFarmCd(feeYr, farmCd);
        if (WiFarmacctY == null) {//将农户水费核定数据拉过来
            WiFarmfeeY wiFarmfeeY = wiFarmfeeYMapper.selectWiFarmfeeYByFeeYrAndFarmCd(feeYr, farmCd);
            if (wiFarmfeeY == null) {
                return null;
            }
            WiFarmacctY wiFarmacctY = new WiFarmacctY();
            wiFarmacctY.setFeeYr(feeYr);
            wiFarmacctY.setFarmCd(farmCd);
            wiFarmacctY.setTotalFee(wiFarmfeeY.getWaterFee());
            wiFarmacctY.setCashNum(0L);
            wiFarmacctY.setPayFee(new BigDecimal("0"));
            wiFarmacctY.setSaveFee(wiFarmfeeY.getWaterFee());
            wiFarmacctYMapper.insertWiFarmacctY(wiFarmacctY);
        }
        WiFarmacctY = wiFarmacctYMapper.selectWiFarmacctYByFeeYrAndFarmCd(feeYr, farmCd);
        String farmNm = wiFarmacctYMapper.getFarmNm(farmCd);
        if (StringUtils.isNotBlank(farmNm)) {
            WiFarmacctY.setFarmNm(farmNm);
        }
        String operNm = wiFarmacctYMapper.getOperNm(WiFarmacctY.getLastOper());
        if (StringUtils.isNotBlank(operNm)) {
            WiFarmacctY.setLastOper(operNm);
        }
        return WiFarmacctY;
    }

    /**
     * 查询农户水费台账登记列表
     *
     * @param wiFarmacctY 农户水费台账登记
     * @return 农户水费台账登记
     */
    @Override
    public List<WiFarmacctY> selectWiFarmacctYList(WiFarmacctY wiFarmacctY) {
        return wiFarmacctYMapper.selectWiFarmacctYList(wiFarmacctY);
    }

    /**
     * 新增农户水费台账登记
     *
     * @param wiFarmacctY 农户水费台账登记
     * @return 结果
     */
    @Override
    public int insertWiFarmacctY(WiFarmacctY wiFarmacctY) {
        return wiFarmacctYMapper.insertWiFarmacctY(wiFarmacctY);
    }

    /**
     * 修改农户水费台账登记
     *
     * @param wiFarmacctY 农户水费台账登记
     * @return 结果
     */
    @Override
    public int updateWiFarmacctY(WiFarmacctY wiFarmacctY) {
        //当手动修改应缴费用时，需要重新核算欠缴费用
//        wiFarmacctY.setSaveFee(wiFarmacctY.getTotalFee().subtract(wiFarmacctY.getPayFee()));
        WiFarmacctY wiFarmacctY1 = new WiFarmacctY();
        wiFarmacctY1.setSaveFee(wiFarmacctY.getTotalFee().subtract(wiFarmacctY.getPayFee()));
        wiFarmacctY1.setTotalFee(wiFarmacctY.getTotalFee());
        wiFarmacctY1.setIsOk(wiFarmacctY.getIsOk());
        wiFarmacctY1.setFeeYr(wiFarmacctY.getFeeYr());
        wiFarmacctY1.setFarmCd(wiFarmacctY.getFarmCd());
        wiFarmacctY1.setLastOper(SecurityUtils.getUserId() + "");
        wiFarmacctY1.setLastTime(new Date());
        wiFarmacctY1.setWriteTime(new Date());
        return wiFarmacctYMapper.updateWiFarmacctY(wiFarmacctY1);
    }

    /**
     * 批量删除农户水费台账登记
     *
     * @param feeYrs 需要删除的农户水费台账登记主键
     * @return 结果
     */
    @Override
    public int deleteWiFarmacctYByFeeYrs(String[] feeYrs) {
        return wiFarmacctYMapper.deleteWiFarmacctYByFeeYrs(feeYrs);
    }

    /**
     * 删除农户水费台账登记信息
     *
     * @param feeYr 农户水费台账登记主键
     * @return 结果
     */
    @Override
    public int deleteWiFarmacctYByFeeYr(String feeYr) {
        return wiFarmacctYMapper.deleteWiFarmacctYByFeeYr(feeYr);
    }

    /**
     * 农户水费收缴
     *
     * @param wiFarmacctY
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int payFarmFee(WiFarmacctY wiFarmacctY) throws Exception {
        WiFarmacctY wiFarmacctY0 = wiFarmacctYMapper.selectWiFarmacctYByFeeYrAndFarmCd(wiFarmacctY.getFeeYr(), wiFarmacctY.getFarmCd());
        if (wiFarmacctY0 == null) {
            return 0;
        }
        WiFarmbookR wiFarmbookR = new WiFarmbookR();
        wiFarmbookR.setFeeYr(wiFarmacctY.getFeeYr());
        wiFarmbookR.setFarmCd(wiFarmacctY.getFarmCd());
        wiFarmbookR.setIsOk("1");
        List<WiFarmbookR> wiFarmbookRS = wiFarmbookRMapper.selectWiFarmbookRList(wiFarmbookR);

        //向流水账添加记录
        wiFarmbookR.setCashNo(Long.parseLong((wiFarmbookRS.size() + 1) + ""));
        wiFarmbookR.setIsOk(wiFarmacctY.getIsOk());
        wiFarmbookR.setCurFee(wiFarmacctY.getPayFee());
        wiFarmbookR.setPayFee(wiFarmacctY.getPayFee());
        wiFarmbookR.setCashNum(Long.parseLong(wiFarmacctY.getVoucherPath().length + ""));
        wiFarmbookR.setAttrUrls(Arrays.toString(wiFarmacctY.getVoucherPath()));
        wiFarmbookR.setOperId(SecurityUtils.getUserId() + "");
        wiFarmbookR.setPayTime(new Date());
        wiFarmbookR.setWriteOper(wiFarmacctY.getLastOper());
        wiFarmbookR.setWriteTime(new Date());
        int i = wiFarmbookRMapper.insertWiFarmbookR(wiFarmbookR);

        WiFarmacctY wiFarmacctY2 = new WiFarmacctY();
        wiFarmacctY2.setFeeYr(wiFarmacctY.getFeeYr());
        wiFarmacctY2.setFarmCd(wiFarmacctY.getFarmCd());
        wiFarmacctY2.setCashNum(Long.parseLong((wiFarmbookRS.size() + 1) + ""));
        wiFarmacctY2.setPayFee(wiFarmacctY0.getPayFee().add(wiFarmacctY.getPayFee()));
        wiFarmacctY2.setSaveFee(wiFarmacctY0.getTotalFee().subtract(wiFarmacctY0.getPayFee().add(wiFarmacctY.getPayFee())));
        wiFarmacctY2.setLastTime(new Date());
        wiFarmacctY2.setLastOper(wiFarmacctY.getLastOper());
        wiFarmacctY2.setWriteTime(new Date());
        if (i == 0 || wiFarmacctYMapper.updateWiFarmacctY(wiFarmacctY2) == 0) {
            throw new Exception("缴费失败");
        }
        return 1;
    }

    /**
     * 农户当年收缴分析
     *
     * @param year
     * @param orgCd
     * @return
     */
    @Override
    public List<WiFarmacctY> farmFeeIncomeAnalys(String year, String orgCd) {
        String orgCdQuery;
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(orgCd);
        orgCdQuery = addvAndOrgCodeInfo.get("resCode") + "";
        List<WiFarmacctY> orgFeeIncomeList = wiFarmacctYMapper.getFarmFeeIncomeAnalys(year, orgCdQuery);
        for (WiFarmacctY vo : orgFeeIncomeList) {
            String farmNm = wiFarmacctYMapper.getFarmNm(vo.getFarmCd());
            if (StringUtils.isNotBlank(farmNm)) {
                vo.setFarmNm(farmNm);
            }
            vo.setCompleteRate(vo.getPayFee().divide(vo.getTotalFee(), 4, BigDecimal.ROUND_HALF_DOWN));
        }
        return orgFeeIncomeList;
    }

    /**
     * 文件上传接口
     *
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        String name = FileUploadUtils.upload(localFilePath, file);
        String url = domain + localFilePrefix + name;
        return url;
    }

    /**
     * 获取渠道下水费收缴情况
     *
     * @param mainCd
     */
    @Override
    public ArrayList<HashMap<String, Object>> getFeeInfoByChan(String mainCd) {
        ArrayList<HashMap<String, Object>> res = new ArrayList<>();
        //查询管辖渠道
        List<MiMainchanB> miMainchanBS = wiFarmacctYMapper.selectChanCds(mainCd);
        for (MiMainchanB miMainchanB : miMainchanBS) {
            HashMap<String, Object> map = new HashMap<>();
            String chanNm = wiFarmacctYMapper.getChanNm(miMainchanB.getChanCd());
            map.put("chanNm", chanNm);
            map.put("chanCd", miMainchanB.getChanCd());
            HashMap<String, Object> chanCodeInfo = BusinessUtils.getChanCodeInfo(miMainchanB.getChanCd());
            String chanCdQuery = chanCodeInfo.get("resCode") + "";
            //查询渠道下缴费情况
            WiFarmacctY wiFarmacctY = wiFarmacctYMapper.getFeeInfoByChanCd(chanCdQuery);
            if (wiFarmacctY != null) {
                map.put("totalFee", wiFarmacctY.getTotalFee());
                map.put("payFee", wiFarmacctY.getPayFee());
                map.put("saveFee", wiFarmacctY.getSaveFee());
            } else {
                map.put("totalFee", 0);
                map.put("payFee", 0);
                map.put("saveFee", 0);
            }
            res.add(map);
        }
        return res;
    }

    /**
     * 获取水管员管辖渠道
     *
     * @param mainCd
     * @return
     */
    @Override
    public List<MiMainchanB> selectChanCds(String mainCd) {
        return wiFarmacctYMapper.selectChanCds(mainCd);
    }

    @Override
    public List<HashMap<String, String>> getFarmCdsByChanCd(String chanCdQuery) {
        ArrayList<HashMap<String, String>> res = new ArrayList<>();
        List<WiFarmacctY> wiFarmacctYS = wiFarmacctYMapper.getFarmCdsByChanCd(chanCdQuery);
        for (WiFarmacctY wiFarmacctY : wiFarmacctYS) {
            HashMap<String, String> map = new HashMap<>();
            map.put("farmCd", wiFarmacctY.getFarmCd());
            map.put("totalFee", wiFarmacctY.getTotalFee() + "");
            map.put("payFee", wiFarmacctY.getPayFee() + "");
            map.put("saveFee", wiFarmacctY.getSaveFee() + "");
            map.put("feeYr", wiFarmacctY.getFeeYr() + "");
            String farmNm = wiFarmacctYMapper.getFarmNm(wiFarmacctY.getFarmCd());
            if (StringUtils.isNotBlank(farmNm)) {
                map.put("farmNm", farmNm);
            }
            res.add(map);
        }
        return res;
    }
}
